package com.xixiake.utils;

import android.app.Activity;
import android.view.Gravity;
import android.widget.Toast;

import com.xixiake.application.BaseApplication;

/**
 * Toast工具类
 * Created by c-wanghengli on 2017/5/28.
 */
public class ToastUtil {

    public static Toast mToast;

    /**
     * Toast显示
     * @param text
     * @param length
     */
    public static void show(final String text,final int length){
        if("main".equalsIgnoreCase(Thread.currentThread().getName())){
            if(mToast == null){
                  mToast = Toast.makeText(BaseApplication.getContext(),text,length);
                  mToast.setGravity(Gravity.CENTER,0,0);
            }else{
                mToast.setGravity(Gravity.CENTER,0,0);
                mToast.setText(text);
                mToast.setDuration(length);
            }
            mToast.show();
        }else{
            ((Activity)BaseApplication.getContext()).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(mToast == null){
                        mToast = Toast.makeText(BaseApplication.getContext(),text,length);
                        mToast.setGravity(Gravity.CENTER,0,0);
                    }else{
                        mToast.setGravity(Gravity.CENTER,0,0);
                        mToast.setText(text);
                        mToast.setDuration(length);
                    }
                    mToast.show();
                }
            });

        }
    }
}
